﻿using IQIGame.Onigao.GameAOT;
using UnityEngine;

namespace IQIGame.Onigao.Game
{
    //UI适配
    public static class UIAdaptation
    {
        public const float StandardWidth = 2340f;
        public const float StandardHeight = 1080f;
        public const float StandardRatio = StandardWidth / StandardHeight;
        public const float BackgroundRatio = 2f;

        public static float ScreenRatio => (float)Screen.width / Screen.height;

        public static float AdaptationCanvasScaler()
        {
            return (ScreenRatio > StandardRatio) ? 1 : 0;
        }

        public static Vector2 GenerateBackgroundCullSize(Vector2 referenceResolution)
        {
            if (ScreenRatio > BackgroundRatio)
            {
                var width = referenceResolution.x;
                return new Vector2(width, width / BackgroundRatio);
            }
            else
            {
                var height = referenceResolution.y;
                return new Vector2(height * BackgroundRatio, height);
            }
        }

        public static Vector2 GenerateMovieCullSize(Vector2 referenceResolution)
        {
            if (ScreenRatio > StandardRatio)
            {
                var width = referenceResolution.x;
                return new Vector2(width, width / StandardRatio);
            }
            else
            {
                var height = referenceResolution.y;
                return new Vector2(height * StandardRatio, height);
            }
        }

        public static Vector2 GenerateBackgroundStretchSize(Vector2 referenceResolution)
        {
            float curScreenRatio = ScreenRatio;
            if (curScreenRatio > StandardRatio)
            {
                var height = referenceResolution.y;
                return new Vector2(height * curScreenRatio, height);
            }
            else
            {
                var width = referenceResolution.x;
                return new Vector2(width, width / curScreenRatio);
            }
        }

        public static bool HasNotch
        {
            get
            {
                var safeArea = Screen.safeArea;
                bool hasNotch = Screen.width > safeArea.width || Screen.height > safeArea.height;
#if UNITY_ANDROID && !UNITY_EDITOR
                hasNotch = hasNotch ? true : GetAndroidNotch();
#endif
                return hasNotch;
            }
        }

        public static float GetDefaultNotchLeftOrTop()
        {
            if (HasNotch)
            {
                float notch = 0;
                var safeArea = Screen.safeArea;
#if UNITY_EDITOR || UNITY_IOS
                notch = (Screen.width - safeArea.width) / 2;
#elif UNITY_ANDROID
                notch = GetAndroidStatusHeight();
#endif
                return notch / Screen.width * 100;
            }
            return 0;
        }

        public static float GetDefaultNotchRightOrBottom()
        {
            if (HasNotch)
            {
                float notch = 0;
                var safeArea = Screen.safeArea;
#if UNITY_EDITOR || UNITY_IOS
                notch = (Screen.width - safeArea.width) / 2;
#elif UNITY_ANDROID
                notch = GetAndroidStatusHeight();
#endif
                return notch / Screen.width * 100;
            }
            return 0;
        }

#if UNITY_ANDROID

        public static float GetAndroidStatusHeight()
        {
            return AndroidInterface.GetStatusHeight(0);
        }

        public static bool GetAndroidNotch()
        {
            var device = DeviceModel();
            if (!string.IsNullOrEmpty(device))
            {
                var method = string.Format("HasNotchIn{0}", device);
                return AndroidInterface.CallStatic_FuncBool(method, false);
            }
            return false;
        }


        static readonly string[] TargetDevice = new string[] { "Huawei", "Xiaomi", "Oppo", "Vivo" };

        static string DeviceModel()
        {
            var deviceName = SystemInfo.deviceModel;
            foreach (var device in TargetDevice)
            {
                if (deviceName.Contains(device, System.StringComparison.OrdinalIgnoreCase))
                    return device;
            }
            return null;
        }
#endif

    }

}
